/**************************************************************
 *
 * Copyright (c) 2008, ******, All Rights Reserved.
 *
 * 名 称:
 *		base_order.cpp
 * 位 置:
 *		src/biz/base_order.cpp
 * 描 述:
 *		基础委托类
 * 备 注:
 *		无
 * 修 改:
 *		2008-09-21  Simon   - 创建
 *
 **************************************************************/

#include <string>
#include <iostream>
#include <exception>
#include "occi.h"
#include "pub/def.h"
#include "pub/str.h"
#include "pub/log.h"
#include "pub/util.h"
#include "pub/db.h"
#include "biz/base_service.h"
#include "biz/base_order.h"

using namespace std;
using namespace oracle::occi;

//----------------------------------------------
//  构造函数
//----------------------------------------------
BaseOrder::BaseOrder()
{
    inAdvanceFlag	= "0";
    inAdvanceDate	= "";
    grantNeeded		= "0";
	grantFlag		= "";
	grantOpID		= "";
	grantDate		= "";
	grantTime		= "";
	orderFromFlag	= "";
	orderStatus		= "0";
}

//----------------------------------------------
//  析构函数
//----------------------------------------------
BaseOrder::~BaseOrder()
{
}

//----------------------------------------------
//  检查输入参数
//----------------------------------------------
void BaseOrder::checkData()
{
	BaseService::checkData();

	//检查交易帐号
	if (!serviceCode.match("^[0-9]{1,10}"))
	{
		Log::throwError("交易帐号不合法");
	}
}

//----------------------------------------------
//  初始化
//----------------------------------------------
void BaseOrder::init()
{
	int count = 0;

	//基础服务初始化
	BaseService::init();

	//获取委托日期、委托时间
	orderDate = tradeDate;
	orderTime = tradeTime;

	Statement* stmt = conn->createStatement();
	ResultSet* rs = 0;
	try
	{
		//检查交易账户是否存在
		stmt->setSQL(" \
			select count(*) \
			from AcctTrans \
			where transAccountID=:transAccountID");
		stmt->setString(1, transAccountID);
		rs = stmt->executeQuery();
		rs->next();
		count = rs->getInt(1);
		if (count <= 0)
		{
			Log::throwError("交易帐号不存在");
		}

		//获取交易账户状态、客户账户
		stmt->setSQL(" \
			select transAcctStatus, custAccountID \
			from AcctTrans \
			where transAccountID = transAccountID");
		stmt->setString(1, transAccountID);
		rs = stmt->executeQuery();
		rs->next();
		transAcctStatus = rs->getString(1);
		custAccountID = rs->getString(2);

		//检查客户账户是否存在
		stmt->setSQL(" \
			select count(*) \
			from AcctCust \
			where custAccountID=:custAccountID");
		stmt->setString(1, custAccountID);
		rs = stmt->executeQuery();
		rs->next();
		count = rs->getInt(1);
		if (count <= 0)
		{
			Log::throwError("客户帐号不存在");
		}

		//获取投资者类型
		stmt->setSQL(" \
			select invstType \
			from AcctCust \
			where custAccountID = custAccountID");
		stmt->setString(1, custAccountID);
		rs = stmt->executeQuery();
		rs->next();
		invstType = rs->getString(1);

		stmt->closeResultSet(rs);
		conn->terminateStatement(stmt);
	}
	catch (...)
	{
		stmt->closeResultSet(rs);
		conn->terminateStatement(stmt);
		throw ;
	}

	//检查经办人信息
	checkTransactor();
}

//----------------------------------------------
//  检查功能权限
//----------------------------------------------
void BaseOrder::checkPriv()
{
    //对服务检查功能权限
	BaseService::checkPriv();

	//检查交易账户状态是否允许执行该业务
	priv->serviceCode		= serviceCode;
	priv->transAcctStatus	= transAcctStatus;
	priv->checkAcctTrans();
}

//----------------------------------------------
//  检查条件是否满足
//----------------------------------------------
void BaseOrder::checkCondition()
{
    //对服务检查条件是否满足
	BaseService::checkCondition();

	//检查交易密码
	checkPassword();
}

//----------------------------------------------
//  写操作日志
//----------------------------------------------
void BaseOrder::writeLog()
{
	log->orderSerialNo	= orderSerialNo;
	BaseService::writeLog();
}

//----------------------------------------------
//  检查交易密码
//----------------------------------------------
void BaseOrder::checkPassword()
{
	int count = 0;

	Statement* stmt = conn->createStatement();
	ResultSet* rs = 0;
	try
	{
		//检查交易密码
		stmt->setSQL(" \
			select count(*) \
			from AcctTrans \
			where transAccountID = :transAccountID \
			and tradePassword = :tradePassword");
		stmt->setString(1, transAccountID);
		stmt->setString(2, tradePassword);
		rs = stmt->executeQuery();
		rs->next();
		count = rs->getInt(1);
		if (count <= 0)
		{
			Log::throwError("交易密码错误.");
		}
		stmt->closeResultSet(rs);
		conn->terminateStatement(stmt);
	}
	catch(...)
	{
		stmt->closeResultSet(rs);
		conn->terminateStatement(stmt);
		throw;
	}
}

//----------------------------------------------
//  检查经办人信息
//----------------------------------------------
void BaseOrder::checkTransactor()
{
	const char* pattern = 0;

	if (invstType == "1")
	{
		return;
	}

	//检查经办人姓名
	pattern = "^.+$";
	if (!instTransName.match(pattern))
	{
		Log::throwError("经办人名称不合法.");
	}

	//检查经办人证件类型
	pattern = "^[0-9]$";
	if (!instTransCertType.match(pattern))
	{
		Log::throwError("经办人证件类型不合法.");
	}

	//检查经办人证件号码
	pattern = "^[0-9]$";
	if (!instTransCertNo.match(pattern))
	{
		Log::throwError("经办人证件号码不合法.");
	}

	//检查经办人证件号码
	pattern = "^([0-9]{8})?$";
	if (!instTransCertValidFrom.match(pattern))
	{
		Log::throwError("经办人证件有效期起始日期不合法.");
	}

	//检查经办人证件有效期截至日期
	pattern = "^([0-9]{8})?$";
	if (!instTransCertValidTo.match(pattern))
	{
		Log::throwError("经办人证件有效期截至日期不合法.");
	}
}
